/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sistema.entidades;

import br.com.sistema.excecoes.EntidadeNaoEncontrada;
import br.com.sistema.infraestrutura.ConnectionPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lauro
 */
public class Foto {

    private int id;
    private String legenda;
    private String extensao;
    protected Connection conexao;

    public Foto(int id, String legenda, String extensao) {
        this.id = id;
        this.legenda = legenda;
        this.extensao = extensao;
    }

    //<editor-fold defaultstate="collapsed" desc="Metodos Getters & Setters.">
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLegenda() {
        return legenda;
    }

    public void setLegenda(String legenda) {
        this.legenda = legenda;
    }

    public String getExtensao() {
        return extensao;
    }

    public void setExtensao(String extensao) {
        this.extensao = extensao;
    }

    public Connection getConexao() {
        if (conexao == null) {
            this.conexao = ConnectionPool.getConnection();
        }
        return conexao;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Strings SQL.">
    private String getSQLInsercao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO foto ( ");
        sql.append("   legenda, ");
        sql.append("   extensao ");
        sql.append(" ) VALUES ( ");
        sql.append("   ?, ");
        sql.append("   ? ");
        sql.append(" ) ");
        return sql.toString();
    }

    private String getSQLAtualizacao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ");
        sql.append(" foto ");
        sql.append(" SET ");
        sql.append(" legenda=?, ");
        sql.append(" extensao=? ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }

    private String getSQLExclusao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" DELETE FROM ");
        sql.append(" foto ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }

    protected String getSQLBuscarPorId() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * ");
        sql.append(" FROM foto ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }

    protected static String getSQLBuscarPorGaleria() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * ");
        sql.append(" FROM foto ");
        sql.append(" WHERE galeria = ? ");
        return sql.toString();
    }
    // </editor-fold>

    // BEGIN: CRUD
    //<editor-fold defaultstate="expanded" desc="Metodos CRUD.">
    public void buscar() throws SQLException, EntidadeNaoEncontrada {
        getConexao();
        PreparedStatement query = getConexao().prepareStatement(getSQLBuscarPorId());
        query.setInt(1, this.getId()); // id
        ResultSet result = query.executeQuery();
        if (result.next()) {
            this.setId(result.getInt("id"));
            this.setLegenda(result.getString("legenda"));
            this.setExtensao(result.getString("extensao"));
        } else {
            throw new EntidadeNaoEncontrada();
        }
    }

    static List<Foto> getFotos(Galeria galeria) throws SQLException {
        List<Foto> fotos = new ArrayList<Foto>();
        Connection conexao = ConnectionPool.getConnection();
        PreparedStatement query = conexao.prepareStatement(getSQLBuscarPorGaleria());
        query.setInt(1, galeria.getId()); // id
        ResultSet result = query.executeQuery();
        while (result.next()) {
            Foto foto = new Foto(result.getInt("id"),
                    result.getString("legenda"),
                    result.getString("extensao"));
            fotos.add(foto);
        }
        return fotos;
    }

    public void salvar() throws SQLException {
        getConexao();
        conexao.setAutoCommit(false);
        inserir();
        conexao.commit();
    }

    private void inserir() throws SQLException {
        PreparedStatement query = getConexao().prepareStatement(getSQLInsercao(), Statement.RETURN_GENERATED_KEYS);
        query.setString(1, this.getLegenda()); // legenda
        query.setString(2, this.getExtensao()); // extensao
        query.executeUpdate();
        ResultSet result = query.getGeneratedKeys();
        result.first();
        int result_id = result.getInt(1);
        this.setId(result_id);
    }

    public void alterar() throws SQLException {
        getConexao();
        conexao.setAutoCommit(false);
        atualizar();
        conexao.commit();
    }

    private void atualizar() throws SQLException {
        PreparedStatement query = getConexao().prepareStatement(getSQLAtualizacao());
        query.setString(1, this.getLegenda()); // legenda
        query.setString(2, this.getExtensao()); // extensao
        query.setInt(3, this.getId()); // id
        query.executeUpdate();
    }

    public void remover() throws SQLException {
        getConexao();
        conexao.setAutoCommit(false);
        excluir();
        conexao.commit();
    }

    private void excluir() throws SQLException {
        PreparedStatement query = getConexao().prepareStatement(getSQLExclusao());
        query.setInt(1, this.getId()); // id
        query.executeUpdate();
    }
    // </editor-fold>
    // END: CRUD
}
